﻿// ===============================================================================
// Project Name        :    Gfw.Repositories.Account
// Project Description :    
// ===============================================================================
// Class Name          :    ActUserBargain4emoneyRepository
// Namespace:          :    Gfw.Repositories.Account
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 10:24:18
// Update Time         :    2012/6/24 10:24:18
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Account;
using Gfw.Repository;
using NHibernate.Criterion;

namespace Gfw.Repositories.Account
{
    public class ActUserBargain4emoneyRepository : Repository<ActUserBargain4emoney>
    {
        /// <summary>
        /// 增加收入记录
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="type"></param>
        /// <param name="amount"></param>
        /// <param name="balance"></param>
        /// <param name="unitOfWork"></param>
        public virtual void AddIncome(string userId, int type, decimal amount, decimal balance, decimal tax,
                                      string awardSerialNo = null, string taxationSerialNo = null,
                                      UnitOfWork unitOfWork = null)
        {
            ActUserBargain4emoney bargain = new ActUserBargain4emoney()
                                                {
                                                    Id = Guid.NewGuid().ToString(),
                                                    Userid = userId,
                                                    TradeType = type,
                                                    Income = amount,
                                                    Outlay = 0,
                                                    Balance = balance,
                                                    TradeDate = DateTime.Now,
                                                    Taxation = tax,
                                                    TaxationSerialNo = taxationSerialNo,
                                                    AwardSerialNo = awardSerialNo
                                                };

            Save(bargain, unitOfWork);
        }


        /// <summary>
        /// 增加支出记录
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="type"></param>
        /// <param name="amount"></param>
        /// <param name="balance"></param>
        /// <param name="unitOfWork"></param>
        public virtual void AddOutlay(string userId, int type, decimal amount, decimal balance,
                                      string awardSerialNo = null, string taxationSerialNo = null,
                                      UnitOfWork unitOfWork = null)
        {
            ActUserBargain4emoney bargain = new ActUserBargain4emoney()
                                                {
                                                    Id = Guid.NewGuid().ToString(),
                                                    Userid = userId,
                                                    TradeType = type,
                                                    Income = 0,
                                                    Outlay = amount,
                                                    Balance = balance,
                                                    TradeDate = DateTime.Now,
                                                    Taxation = 0,
                                                    TaxationSerialNo = taxationSerialNo,
                                                    AwardSerialNo = awardSerialNo
                                                };

            Save(bargain, unitOfWork);
        }

        public virtual IList<ActUserBargain4emoney> Query(string userId,int[] tradeTypes,DateTime? start,DateTime? end,int pageIndex,int pageSize,out int totalPage)
        {
            var query = Session.CreateCriteria("ActUserBargain4emoney").Add(Expression.Eq("Userid", userId));
            if (start.HasValue)
                query = query.Add(Expression.Ge("TradeDate", start.Value));
            if (end.HasValue)
            {
                end = end.Value.AddDays(1);
                query = query.Add(Expression.Le("TradeDate", end.Value));
            }

            if(tradeTypes != null)
                query = query.Add(Expression.In("TradeType", tradeTypes));

            query = query.AddOrder(new Order("TradeDate", false));
           
            totalPage = (query.List().Count + pageSize - 1)/pageSize;
          return  query.SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize).List<ActUserBargain4emoney>();
        }
    }
}
